package com.xiaohai.big_event_system.converter;

import com.xiaohai.big_event_system.enums.Gender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Slf4j
@Component
public class GenderEnumConverter implements Converter<String, Gender> {
    @Override
    public Gender convert(String source) {
        log.info("GenderEnumConverter convert source:{}", source);
        Integer value = Integer.parseInt(source);
//        log.info("GenderEnumConverter convert source:{}", value);
//        for (GenderEnum gender : GenderEnum.values()) {
//            if (gender.getValue().equals(value)) {
//                return gender;
//            }
//        }
//       throw  new IllegalArgumentException("无法转换枚举");
        return Arrays.stream(Gender.values())
                .filter(gender -> gender.getValue().equals(value))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("无法转换枚举"));
    }
}
